﻿

//页面跳转
function skipPage(self, index) {
    if (index == "0") {
        self.activeIndex = "1";
        index = "1";
    }
    switch (index) {
        case "0":
        case "1":
            window.location.href = "/main/index";
            break;
        case "2":
            window.location.href = "/main/myAlbum";
            break;
        case "3":
            showMessage(self, '套餐服务正在努力开发中...');
            break;
        case "4":
            showMessage(self, '客户端正在努力开发中...', true, 'warning');
            break;
        case "5":
            window.location.href = "/main/login";
            break;
        case "6":
            window.location.href = "/main/register";
            break;
        case "7":
            window.location.href = "/main/setting";
            break;
        case "8-1":
            window.location.href = "/main/myAccount";
            break;
        case "8-2":
            logout(self)
            break;
    }
}

//公共函数
var PF = {
    //我的相册下拉框
    dropdownalbums(self) {
        Axios.get(self, '/main/albums', {
            success(res) {
                self.defaultalbum = '默认相册';
                self.dropalbums = res.data;
            }
        });
    }
};

//退登
var logout = function (self) {
    self.$confirm('你确认要退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
    }).then(() => {
        Axios.get(self, '/main/logout', {
            success(res) {
                window.location.href = '/';
            }
        });
    }).catch(() => {

    });
}


//封装消息提示
var showMessage = function (self, msg, isSuccess = false, type = 'success') {
    if (isSuccess) {
        self.$message({
            message: msg,
            type: type
        });
    } else {
        self.$message.error('提示：' + msg);
    }
}


var DateHelp = {
    //Json格式转换
    convertTime: function (jsonTime, format) {
        var date = new Date(parseInt(jsonTime.replace("/Date(", "").replace(")/", ""), 10));
        var formatDate = date.format(format);
        return formatDate;
    },
    //最近一个月
    latelyMonth() {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        var arry = [start, end];
        return arry;
    }
}

//封装Axios请求
var Axios = {
    //get请求
    get: function (self, url, params) {
        var isLoding = params.loading == undefined ? true : false;//加遮罩
        self.loading = isLoding;
        axios.get(url, {
            params: params.data
        }).then(function (response) {
            if (response.status == 200) {
                if (response.data.code == 200) {//请求成功
                    if ($.isFunction(params.success)) {
                        params.success.call(this, response.data);
                    } else {
                        showMessage(self, response.data.msg, true);
                    }
                } else {
                    if ($.isFunction(params.error)) {
                        params.error.call(this, response.data);
                    } else {
                        showMessage(self, response.data.msg);
                    }
                }
            } else {
                if ($.isFunction(params.error)) {
                    params.error.call();
                }
            }
            if (isLoding) {
                self.loading = false;
            }
        }).catch(function (error) {
            if (isLoding) {
                self.loading = false;
            }
            console.log(error);
            showMessage(self, "网络错误,请稍后再试!");
            if ($.isFunction(params.error)) {
                params.error.call();
            }
        });
    },
    //post请求
    post: function (self, url, params) {
        var isLoading = params.loading == undefined ? true : false;//加遮罩
        self.loading = isLoading;
        axios.post(url, params.data)
            .then(function (response) {
                if (response.status == 200) {
                    if (response.data.code == 200) {
                        if ($.isFunction(params.success)) {
                            params.success.call(this, response.data);
                        } else {
                            showMessage(self, response.data.msg, true);
                        }
                    } else {
                        if ($.isFunction(params.error)) {
                            params.error.call(this, response.data);
                        } else {
                            showMessage(self, response.data.msg);
                        }
                    }
                } else {
                    if ($.isFunction(params.error)) {
                        params.error.call();
                    }
                }
                if (isLoading) {
                    self.loading = false;
                }
            })
            .catch(function (error) {
                if (isLoading) {
                    self.loading = false;
                }
                console.log(error);
                showMessage(self, "操作失败：网络错误,请稍后再试!");
                if ($.isFunction(params.error)) {
                    params.error.call(this, error);
                }
            });
    }
};